package com.ogawa.project6602.activity

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.ogawa.projectcommon.constants.UrlPathConstant
import com.ogawa.project6602.R
import com.ogawa.project6602.base.Base6602MqttActivity
import com.ogawa.project6602.bean.Massage6602Armchair


@Route(path = UrlPathConstant.ACTIVITY_6602_HEALTH_LIFE)
class Device6602HealthLifeActivity : Base6602MqttActivity(), View.OnClickListener {
    private lateinit var tvBall: TextView
    private lateinit var tvGolf: TextView
    private lateinit var tvSwim: TextView
    private lateinit var tvJianzou: TextView
    private lateinit var tvRide: TextView
    private lateinit var tvJianshen: TextView
    private lateinit var tvRunning: TextView
    private var tvCurrent: TextView? = null
    private var program: String = ""

    override fun getLayoutId(): Int {
        return R.layout.activity_6602_health_life
    }

    override fun initView() {
        super.initView()
        setTitle(getString(R.string.health_life))
        tvBall = findViewById(R.id.tv_ball)
        tvBall.setOnClickListener(this)
        tvGolf = findViewById(R.id.tv_golf)
        tvGolf.setOnClickListener(this)
        tvSwim = findViewById(R.id.tv_swimming)
        tvSwim.setOnClickListener(this)
        tvJianzou = findViewById(R.id.tv_jianzou)
        tvJianzou.setOnClickListener(this)
        tvRide = findViewById(R.id.tv_riding)
        tvRide.setOnClickListener(this)
        tvJianshen = findViewById(R.id.tv_jianshen)
        tvJianshen.setOnClickListener(this)
        tvRunning = findViewById(R.id.tv_running)
        tvRunning.setOnClickListener(this)
        findViewById<TextView>(R.id.tv_cancel).setOnClickListener(this)
        findViewById<TextView>(R.id.tv_confirm).setOnClickListener(this)
        setProgram()
    }

    private fun setProgram() {
        val deviceStatusBean = Massage6602Armchair.getInstance().deviceStatusBean
        if (deviceStatusBean != null) {
            val health = Integer.parseInt(deviceStatusBean.functions.lifeAutoProgram.modelValue)
            tvBall.isSelected = health == 1
            tvGolf.isSelected = health == 2
            tvSwim.isSelected = health == 3
            tvJianzou.isSelected = health == 4
            tvRide.isSelected = health == 5
            tvJianshen.isSelected = health == 6
            tvRunning.isSelected = health == 7
            when (health) {
                1 -> tvCurrent = tvBall
                2 -> tvCurrent = tvGolf
                3 -> tvCurrent = tvSwim
                4 -> tvCurrent = tvJianzou
                5 -> tvCurrent = tvRide
                6 -> tvCurrent = tvJianshen
                7 -> tvCurrent = tvRunning
            }
        }
    }

    override fun onClick(v: View?) {
        if (v != tvCurrent) {
            v?.isSelected = true
            tvCurrent?.isSelected = false
            tvCurrent = v as TextView?
        }
        when (v?.id) {
            R.id.tv_ball -> program = "1"
            R.id.tv_golf -> program = "2"
            R.id.tv_swimming -> program = "3"
            R.id.tv_jianzou -> program = "4"
            R.id.tv_riding -> program = "5"
            R.id.tv_jianshen -> program = "6"
            R.id.tv_running -> program = "7"
            R.id.tv_cancel -> {
//                program = ""
                finish()
            }
            R.id.tv_confirm -> {
                if (program.isNotEmpty()) {
                    setResult(Activity.RESULT_OK, Intent().putExtra("program", program))
                }
                finish()
            }
        }
    }
}